package AssetDisposal

import (
	"ToolLibray/jwtx"
	"context"
	"github.com/zeromicro/go-zero/core/logx"
	"property_api/internal/svc"
	"property_api/internal/types"
	"property_api/utils/result"
	"property_api/utils/xform"
	"property_service/property_service"
)

type AddLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddLogic {
	return &AddLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

// Add 添加资产报废的逻辑
func (l *AddLogic) Add(req *types.AssetDisposal) (resp *types.Response, err error) {
	// 设置租户ID，从上下文中获取
	tenantID := jwtx.GetUserTenantId64(l.ctx)
	// 设置创建用户和更新用户，从上下文中获取
	userIdStr := xform.Int64ToString(jwtx.GetUserId(l.ctx))

	// 调用服务层方法，添加资产报废记录
	res, err := l.svcCtx.InventoryService.AssetDisposalAdd(l.ctx, &property_service.AssetDisposal{
		DisposalCode:        req.DisposalCode,        // 报废单号
		AssetID:             req.AssetID,             // 资产ID
		AssetCode:           req.AssetCode,           // 资产编号
		AssetName:           req.AssetName,           // 资产名称
		SpecModel:           req.SpecModel,           // 规格型号
		OriginalValue:       req.OriginalValue,       // 原值(元)
		NetValue:            req.NetValue,            // 净值(元)
		AcceptanceDate:      req.AcceptanceDate,      // 验收日期
		AssetCategory:       req.AssetCategory,       // 资产分类
		AssetCategoryID:     req.AssetCategoryID,     // 资产分类ID
		FinancialCategory:   req.FinancialCategory,   // 财务分类
		FinancialCategoryID: req.FinancialCategoryID, // 财务分类ID
		UsingDept:           req.UsingDept,           // 使用科室
		UsingDeptID:         req.UsingDeptID,         // 使用科室ID
		DeptResponsible:     req.DeptResponsible,     // 科室责任人
		DeptResponsibleID:   req.DeptResponsibleID,   // 科室责任人ID
		Unit:                req.Unit,                // 单位
		StateAssetCode:      req.StateAssetCode,      // 国资编码
		IsRetained:          req.IsRetained,          // 科室是否留用
		DisposalReason:      req.DisposalReason,      // 报废原因备注
		Attachment:          req.Attachment,          // 附件
		StatusID:            req.StatusID,            // 状态ID
		StatusName:          req.StatusName,          // 采购状态
		DisposalStatus:      req.DisposalStatus,      // 状态
		StockID:             req.StockID,             // 仓库ID
		TenantID:            tenantID,                // 租户ID
		CreatedAtuser:       userIdStr,               // 创建人
		UpdatedAtuser:       userIdStr,               // 更新人
	})

	// 如果服务层返回错误，返回统一的错误响应
	if err != nil {
		return result.Err(err).End()
	}

	// 返回成功的响应
	return result.Suc(res).End()
}
